//
//  LevelSelectScene.m
//  Project 5
//
//  Created by Nguyen Tan Loc on 7/6/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "LevelSelectScene.h"
#import "MainGameScene.h"
#import "MenuScene.h"

@implementation LevelSelectScene

+(CCScene *) scene
{
	CCScene *scene = [CCScene node];
	LevelSelectScene *layer = [LevelSelectScene node];
	[scene addChild: layer];
	return scene;
}


-(id) init
{
	if(self = [super init])
    {
        CCMenu *mMenu = [[CCMenu alloc] init];
        [mMenu setPosition:CGPointZero];
        [self addChild:mMenu];
        
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        
        int x = 100;
        int y = winSize.height - 100;
        for (int i = 0; i < 20; i++)
        {
            x = 120 + (i % 5) * 180;
            y = winSize.height - 120 - (i / 5) * 150;
            
            CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:FRAME_NAME_SPLASH];
            
            CCMenuItem *mi = [CCMenuItemSprite itemWithNormalSprite:sprite selectedSprite:nil target:self selector:@selector(onItemClick:)];
            [mi setPosition:ccp(x, y)];
            [mMenu addChild:mi z:0 tag:i];
        }
        
        CCSprite *spriteUpFacebook = [CCSprite spriteWithSpriteFrameName:@"coin@2x.png"];
        CCMenuItem *miTwitter = [CCMenuItemSprite itemWithNormalSprite:spriteUpFacebook selectedSprite:nil target:self selector:@selector(onBack)];
        [miTwitter setPosition:ccp(winSize.width - 40, 50)];
        [mMenu addChild:miTwitter];
	}
    
	return self;
}

-(void) onItemClick:(NSObject *)sender
{
    CCMenuItem *mi = (CCMenuItem *)sender;
    CCLOG(@"tag = %d", mi.tag);
    
    [[LevelManager shareLevelManager] setCurrentLevelt:mi.tag];
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[MainGameScene scene]]];
}

-(void) onBack
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[MenuScene scene]]];
}

@end
